import { defineStore } from 'pinia'
type Opts = { label: string; value: number }
import _ from 'lodash'
import { GetVehiclePowerType } from '@/api/tables'
export const useOptsStore = defineStore('useOptsStore', () => {
  let opts = ref<Opts[]>([])
  const updateOpts = (arr: Opts[]) => {
    arr.map((u) => {
      opts.value.push(u)
    })
    // opts.value = arr
    // console.log('first', opts)
  }

  // let obj = reactive<{ powerType: Opts[] }>({
  //   powerType: [],
  // })
  let powerType = ref<Opts[]>([])
  const formatter = (data: any, [key1 = 'label', key2 = 'value']) => {
    return data.map((item: { [x: string]: any }) => {
      return {
        label: item[key1],
        value: item[key2],
      }
    })
  }
  // /basic/Vehicle/GetVehiclePowerType
  const getPowerType = () => {
    let params = { id: 159 }
    GetVehiclePowerType(params).then((data: any) => {
      powerType.value = formatter(data, ['key', 'value'])
    })
  }
  // let { powerType } = toRefs(obj)
  return {
    opts,
    updateOpts,
    powerType,
    getPowerType,
  }
})
